<?php

namespace app\admin\controller;

use think\facade\Request;
use app\common\Common;
use app\admin\service\VideoService;
use app\common\StsService;

class Video extends Base
{
    private $service;

    public function __construct(VideoService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染短剧列表
    public function index()
    {
        return view();
    }

    // 渲染短剧详情
    public function info()
    {
        return view();
    }

    /**
     * 短剧列表
     */
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getVideoList($params);

        return Common::success($data);
    }

    /**
     * 添加短剧
     */
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editVideo($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 编辑短剧
     */
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editVideo($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 短剧详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getVideoDetail($params['id']);

        return Common::success($data);
    }

    /**
     * 删除视频
     */
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteVideo($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 短剧上下架
    public function shelve()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('shelve')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->videoUpperAndLowerShelves($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    //短剧切流
    public function getFlow(){
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('shelve')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->getFlow($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 渲染短剧统计页面
    public function statistics()
    {
        return view();
    }

    // 短剧统计列表
    public function statisticsList()
    {
        $params = Request::param();

        $data = $this->service->getVideoStatisticsList($params);

        return Common::success($data);
    }

    // 渲染短剧统计详细页面
    public function statisticsInfo()
    {
        return view();
    }

    // 短剧统计详情
    public function statisticsDetail()
    {
        $params = Request::param();

        $data = $this->service->getVideoStatisticsDetail($params);

        return Common::success($data);
    }

    /**
     * 图片文件直传凭证
     * 
     * @date 2022/09/22
     * @return json
     */
    public function uploadCredentials()
    {
        $object = new StsService();
        $data = $object->getStsAuth();

        if ($data['statusCode'] != 200) {
            return Common::fail($data['errorMessage']);
        }

        return Common::success($data);
    }

    // 短剧下拉列表
    public function select()
    {
        $params = Request::param();
        $data = $this->service->getVideoSelectList($params);

        return Common::success($data);
    }

    // 所有短剧下拉列表
    public function selectAll()
    {
        $params = Request::param();
        $data = $this->service->getVideoSelectList($params, null);

        return Common::success($data);
    }

    // 短剧非复制剧下拉列表
    public function selectNoCopy()
    {
        $params = Request::param();
        $data = $this->service->getVideoNoCopySelectList($params);

        return Common::success($data);
    }

    // 指定发布者的短剧下拉列表
    public function publisherVideoSelectList()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('publisher_video_select_list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getPublisherVideoSelectList($params['admin_uid']);

        return Common::success($data);
    }

    // 设为/取消推荐
    public function recommend()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Video::class)->scene('recommend')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->setAsRecommended($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 清理短剧缓存
    public function clear()
    {
        $result = $this->service->clearVideoCache();

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 渲染短剧的人均充值页面
    public function perCapita()
    {
        return view();
    }

    // 获取短剧的人均充值
    public function getVideoPerCapita()
    {
        $params = Request::param();

        $data = $this->service->getPerCapitaByVideo($params);

        return Common::success($data);
    }
}